package com.zdy.syn;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 逸足天涯
 * on 3/9/2020.
 */
public class UnsafeList {
    public static void main(String[] args) {
        List<String> lists = new ArrayList<>() ;
        //理论上list应该是1000，但是多线程进行数据覆盖，实际值小于1000,。线程不安全。
        for (int i = 0; i < 10000; i++) {
            new Thread(()->{
                synchronized (lists){
                    lists.add(Thread.currentThread().getName());
                }
            }).start();
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(lists.size());
    }
}
